<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- Card 卡片 -->
      <el-card class="box-card">
        <!-- Tabs 标签页 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- Layout 布局 -->
            <el-row class="department-header">
              <!-- 左侧区域 -->
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <!-- 右侧区域 -->
              <el-col :span="4">
                <el-row type="flex">
                  <el-col>负责人</el-col>
                  <el-col>
                    <!-- Dropdown 下拉菜单 -->
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="addDepts('')"
                        >添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- Tree 树形控件 -->
            <el-tree
              default-expand-all
              :data="treeData"
              :props="defaultProps"
              class="departments-tree"
            >
              <!-- 使用插槽 -->
              <template v-slot="{ data }">
                <!-- Layout 布局 -->
                <el-row type="flex" justify="space-between" style="width: 100%">
                  <!-- 左侧区域 -->
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <!-- 右侧区域 -->
                  <el-col :span="4">
                    <el-row type="flex">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <!-- Dropdown 下拉菜单 -->
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                              @click.native="addDepts(data.id)"
                            >添加子部门</el-dropdown-item>
                            <el-dropdown-item
                              @click.native="editDept(data.id)"
                            >编辑部门</el-dropdown-item>
                            <el-dropdown-item
                              v-if="data && !data.children"
                              @click.native="delDept(data.id)"
                            >删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <!-- Dialog 对话框 -->
        <el-dialog
          :title="isEdit ? '编辑部门' : '添加部门'"
          :visible.sync="showDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <dept-dialog
            v-if="showDialog"
            :parent-id="parentId"
            :is-edit="isEdit"
            :origin-list="originList"
            @update-list="updateList"
            @close="closeDialog"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartmentsAPI, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import DeptDialog from './deptDialog.vue'
export default {
  components: {
    DeptDialog
  },
  data() {
    return {
      activeName: 'first',
      treeData: [], // 组织架构数据
      // defaultProps属性对应的是数据源里面的字段
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 弹出框的显示
      parentId: '', // 点击添加子部门的父级id
      isEdit: false, // 是否是编辑
      originList: []
    }
  },
  created() {
    // 调用组织架构
    this.getDepartments()
  },
  methods: {
    // 获取组织架构
    async getDepartments() {
      const res = await getDepartmentsAPI()
      // 格式化需要传递给子组件的数据
      this.originList = res.data.depts.map(item => ({
        name: item.name,
        code: item.code,
        id: item.id,
        pid: item.pid
      }))
      // 将后台数据赋值给data变量
      this.treeData = tranListToTreeData(res.data.depts) // 将扁平数组转换为树形结构
    },

    // 点击添加子部门开启弹框
    addDepts(id) {
      this.parentId = id
      this.showDialog = true
      this.isEdit = false
    },

    // 点击编辑组件
    editDept(id) {
      this.parentId = id
      this.showDialog = true
      this.isEdit = true
    },

    // 点击确定关闭弹框
    updateList() {
      // 关闭弹框
      this.showDialog = false
      // 调用组织架构,重新渲染页面
      this.getDepartments()
    },

    // 点击取消关闭弹窗
    closeDialog() {
      this.showDialog = false
    },

    // 删除部门
    async delDept(id) {
      // 显示提示框
      const delRes = await this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 点击取消
      if (delRes === 'cancel') return this.$message('你取消了删除')
      // 点击确定
      const res = await delDepartment(id)
      // 删除成功给用户提示
      this.$message.success(res.message)
      // 重新渲染页面
      this.getDepartments()
    }
  }
}
</script>

<style lang="scss" scoped>
.departments-tree {
  margin: 12px 0px;
}
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>
